import {Component} from '@angular/core';
import {environment} from '../../environments/environment';
import {SystemRes} from '../base/base.model';
import {Router} from '@angular/router';
import {HomeService} from './home.service';
import {CommonTool} from '../common/common.tool';
import {NzIconService, NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent {
  public menus: Array<any>;
  public isCollapsed: boolean;
  public system: SystemRes;
  public isPassWordVisible: boolean;
  public isMaterialVisible: boolean;
  public isAppSecretVisible: boolean;
  public isInfoVisible = false;
  public passwordInfo: {
    password: string
    newPassword: string
    againPassword: string
  };
  public passwordConfig: {
    password: boolean,
    newPassword: boolean
    againPassword: boolean
  };
  public userInfo: {
    appName: string
    oldAppName: string
    account: string
    appId: string
    appSecret: string
    cloudSpace: number
    useSpace: number
    freeFlow: number
    integral: number
    shareCode: string
  };
  public isUpdateSecret: boolean;
  public url = environment.config.url + '/share/page/';

  constructor(private router: Router,
              public homeService: HomeService,
              public commonTool: CommonTool,
              private message: NzMessageService,
              public iconService: NzIconService) {
    this.iconService.fetchFromIconfont({
      scriptUrl: '//at.alicdn.com/t/font_1580957_uw99thxuc6e.js'
    });
    this.system = JSON.parse(sessionStorage.getItem(environment.systemInfo));
    this.menus = [
      {
        level: 1,
        title: '网盘管理',
        icon: 'file',
        open: false,
        selected: false,
        disabled: false,
        children: [
          {
            level: 2,
            title: '云盘',
            icon: 'cloud',
            selected: false,
            disabled: false,
            open: false,
            url: '/home/file'
          },
          {
            level: 2,
            title: '共享文件',
            icon: 'share-alt',
            selected: false,
            disabled: false,
            open: false,
            url: '/home/file/share'
          },
          {
            level: 2,
            title: '安全链接',
            icon: 'link',
            selected: false,
            disabled: false,
            open: false,
            url: '/home/file/safetyChain'
          }
        ]
      },
      // {
      //   level: 1,
      //   title: '博客管理',
      //   icon: 'file',
      //   open: false,
      //   selected: false,
      //   disabled: false,
      //   children: [
      //     {
      //       level: 2,
      //       title: '文章',
      //       icon: 'cloud',
      //       selected: false,
      //       disabled: false,
      //       open: false,
      //       url: '/home/blog'
      //     },
      //     {
      //       level: 2,
      //       title: '评论',
      //       icon: 'share-alt',
      //       selected: false,
      //       disabled: false,
      //       open: false,
      //       url: '/home/blog/comment'
      //     },
      //     {
      //       level: 2,
      //       title: '分类',
      //       icon: 'share-alt',
      //       selected: false,
      //       disabled: false,
      //       open: false,
      //       url: '/home/blog/articleType'
      //     }
      //   ]
      // }
    ];
    this.isPassWordVisible = false;
    this.isMaterialVisible = false;
    this.isAppSecretVisible = false;
    this.userInfo = {
      appName: '',
      oldAppName: '',
      account: '',
      appId: '',
      appSecret: '',
      cloudSpace: 0,
      useSpace: 0,
      freeFlow: 0,
      integral: 0,
      shareCode: ''
    };
    this.passwordInfo = {password: '', newPassword: '', againPassword: ''};
    this.passwordConfig = {password: false, newPassword: false, againPassword: false};
    this.homeService.getUserByToken().subscribe((res) => {
      this.userInfo = res;
      this.userInfo.oldAppName = res.appName;
    });
    this.isUpdateSecret = false;
  }

  public jump(url) {
    if (url) {
      this.router.navigate([url]);
    }
  }


  public toggleCollapsed() {
    this.isCollapsed = !this.isCollapsed;
  }

  // 修改密码关闭
  public modifyPassWordCancel() {
    this.isPassWordVisible = false;
    this.passwordInfo = {password: '', newPassword: '', againPassword: ''};
    this.passwordConfig = {password: false, newPassword: false, againPassword: false};
  }


  // 修改密码提交
  public modifyPassWordOk(data) {
    this.homeService.updatePassword({newPassword: data.newPassword, password: data.password}).subscribe(() => {
      this.message.success('密码修改成功！');
      this.isPassWordVisible = false;
    });
  }

  // 修改资料关闭
  public modifyMaterialCancel() {
    this.isMaterialVisible = false;
    this.userInfo.appName = this.userInfo.oldAppName;
  }

  // 修改资料提交
  public modifyMaterialOk(userInfo) {
    this.homeService.updateUserInfo({appName: userInfo.appName, isUpdateSecret: this.isUpdateSecret}).subscribe(res => {
      this.message.success('资料修改成功！');
      this.isMaterialVisible = false;
    });
  }

  // 退出登录
  public logOut() {
    this.homeService.logOut().subscribe(() => {
      this.commonTool.deleteCookie(environment.systemToken);
      sessionStorage.removeItem(environment.systemToken);
      this.router.navigate(['/login']);
    });
  }

  public query() {
    this.isInfoVisible = true;
    this.homeService.getUserByToken().subscribe((res) => {
      this.userInfo = res;
      this.userInfo.oldAppName = res.appName;
    });
  }

  /**
   * 复制
   */
  public copy(data, html, doc) {
    const input = document.createElement('input');
    input.value = data;
    document.body.appendChild(input);
    input.select();
    if (document.execCommand('Copy')) {
      doc.style.display = 'none';
      html.style.display = 'inline-block';
    }
    input.style.display = 'none';
  }

  /**
   * 复制
   */
  public copyText(data) {
    const input = document.createElement('input');
    input.value = data;
    document.body.appendChild(input);
    input.select();
    if (document.execCommand('Copy')) {
      this.message.success('复制成功！');
    } else {
      this.message.success('复制失败！');
    }
  }
}
